package com.puppet.building182.service.impl;

import com.puppet.building182.annotation.RequirePermCheck;
import com.puppet.building182.handler.MyAssert;
import com.puppet.building182.mapper.BuildingMapper;
import com.puppet.building182.mapper.HouseMapper;
import com.puppet.building182.pojo.Building;
import com.puppet.building182.pojo.House;
import com.puppet.building182.pojo.Result;
import com.puppet.building182.pojo.VO.BuildingVo;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.BuildingService;
import com.puppet.building182.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
 public class BuildingServiceImpl implements BuildingService {
    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    HouseService houseService;

    @Override
    public List<Building> getAllBuildings(String search) {
        return buildingMapper.getAllBuildings(search);
    }

    @Override
    @RequirePermCheck("building:add")
    public Integer addBuilding(Building building) {
        return buildingMapper.addBuilding(building);
    }

    @Override
    @RequirePermCheck("building:del")
    public Integer deleteBuildingById(Integer buildingId) {
        Integer num = houseService.countHouseByBuildingId(buildingId);
        MyAssert.error(num > 0, BuildingCode.DELETE_BUILD_FAIL.getMessage());
        return buildingMapper.deleteBuildingById(buildingId);
    }

    @Override
    public Integer deleteBuildings(List<Integer> buildings) {
        for (Integer buildingId : buildings) {
            Integer num = houseService.countHouseByBuildingId(buildingId);
            MyAssert.error(num > 0, BuildingCode.DELETE_BUILD_FAIL.getMessage());
        }
        return buildingMapper.deleteBuildings(buildings);
    }

    @Override
    @RequirePermCheck("building:update")
    public Integer updateBuilding(Building building) {
        return buildingMapper.updateBuilding(building);
    }

    @Override
    public Building getBuildingById(Integer uId) {
        return buildingMapper.getBuildingById(uId);
    }

    @Override
    public List<BuildingVo> getBuildingNameAndId() {
        return buildingMapper.getBuildingNameAndId();
    }
}
